﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Entity.Package;
using Tools;

namespace BLL.Business
{
    public class ProcessBuiness
    {
        /// <summary>
        /// 按照交易码，确定请求包类型，并调用不同的业务处理函数。
        /// </summary>
        /// <param name="body"></param>
        public static void DistributeBusiness(byte[] body,PackHead pkHead,CSocket.ClientThread client)
        {
            string tradeCode = CSocket.PackageHandle.GetTradeCode(body);
            switch ((TradeCode)int.Parse(tradeCode))
            { 
                case TradeCode.对明细帐:
                    GenericDetailAccount(body,pkHead,client);
                    break;
                case TradeCode.缴费查询:
                    GenericPaySearch(body,pkHead,client);
                    break;
                case TradeCode.缴费冲帐:
                    GenericStrikeBalance(body,pkHead,client);
                    break;
                case TradeCode.缴费业务:
                    GenericPayment(body,pkHead,client);
                    break;
            }
            
        }

        /// <summary>
        /// 生成明细对帐实体对象，并进行业务处理
        /// </summary>
        /// <param name="body"></param>
        private static void GenericDetailAccount(byte[] body, PackHead pkHead, CSocket.ClientThread client)
        {

            PackFailReturn pkFailReturn = new PackFailReturn();
            pkFailReturn.TradeCode = ((int)TradeCode.对明细帐).ToString();
            try
            {
                object obj = CSocket.PackageHandle.GetRequestBody(body);
                if (obj == null)
                {
                    pkFailReturn.ResponseCode = ((int)ResponseCode.数据包格式错).ToString().PadLeft(3,'0');
                    pkFailReturn.PackHead = new PackHead(pkHead.PayWay, pkHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                    CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
                    LogHelper.WriteInfoLog("GenericDetailAccount方法返回020响应码");
                    return;
                }
                PackRequestDetailAccount packDZ = (PackRequestDetailAccount)obj;
                packDZ.packHead = pkHead;
                DetailAccount.CheckDetailAccount(packDZ, client);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("GenericDetailAccount方法-生成对账信息实体对象异常：" + ex.Message, ex);
                pkFailReturn.ResponseCode = ((int)ResponseCode.其他错误).ToString().PadLeft(3,'0');
                pkFailReturn.PackHead = new PackHead(pkHead.PayWay, pkHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
            }  
        }

        /// <summary>
        /// 生成冲帐实体对象，并进行业务处理
        /// </summary>
        /// <param name="body"></param>
        private static void GenericStrikeBalance(byte[] body,PackHead pkHead, CSocket.ClientThread client)
        {
            PackFailReturn pkFailReturn = new PackFailReturn();
            pkFailReturn.TradeCode = ((int)TradeCode.缴费冲帐).ToString();
            try
            {
                object obj = CSocket.PackageHandle.GetRequestBody(body);
                if (obj == null)
                {
                    pkFailReturn.ResponseCode = ((int)ResponseCode.数据包格式错).ToString().PadLeft(3,'0');
                    pkFailReturn.PackHead = new PackHead(pkHead.PayWay, pkHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                    CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
                    LogHelper.WriteInfoLog("GenericStrikeBalance方法返回020响应码");
                    return;
                }
                PackRequestStrikeBalance packStrike = (PackRequestStrikeBalance)obj;
                packStrike.packHead = pkHead;
                StrikeBalance.ProcessStrikeBalance(packStrike, client);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("GenericStrikeBalance方法-生成冲正信息实体对象异常：" + ex.Message,ex);
                pkFailReturn.ResponseCode = ((int)ResponseCode.其他错误).ToString().PadLeft(3,'0');
                pkFailReturn.PackHead = new PackHead(pkHead.PayWay, pkHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
            }                       
        }

        /// <summary>
        /// 生成查询信息实体对象，并进行业务处理
        /// </summary>
        /// <param name="body"></param>
        private static void GenericPaySearch(byte[] body,PackHead pkHead, CSocket.ClientThread client)
        {
            PackFailReturn pkFailReturn = new PackFailReturn();
            pkFailReturn.TradeCode = ((int)TradeCode.缴费查询).ToString();
            try
            {
                object obj = CSocket.PackageHandle.GetRequestBody(body);
                if (obj == null)
                {
                    pkFailReturn.ResponseCode = ((int)ResponseCode.数据包格式错).ToString().PadLeft(3,'0');
                    pkFailReturn.PackHead = new PackHead(pkHead.PayWay, pkHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                    CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
                    LogHelper.WriteInfoLog("GenericPaySearch方法返回020响应码");
                    return;
                }
                PackRequestSearch search = (PackRequestSearch)obj;
                search.packHead = pkHead;
                SearchUserInfo.GetGasUserInfo(search, client); 
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("GenericPaySearch方法-生成查询信息实体对象异常：" + ex.Message, ex);
                pkFailReturn.ResponseCode = ((int)ResponseCode.其他错误).ToString().PadLeft(3,'0');
                pkFailReturn.PackHead = new PackHead(pkHead.PayWay, pkHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
            }
                      
        }

        /// <summary>
        /// 生成缴费信息实体对象，并进行业务处理。
        /// </summary>
        /// <param name="body"></param>
        private static void GenericPayment(byte[] body,PackHead pkHead, CSocket.ClientThread client)
        {
            PackFailReturn pkFailReturn = new PackFailReturn();
            pkFailReturn.TradeCode = ((int)TradeCode.缴费业务).ToString();
            try
            {
                object obj = CSocket.PackageHandle.GetRequestBody(body);
                if (obj == null)
                {
                    pkFailReturn.ResponseCode = ((int)ResponseCode.数据包格式错).ToString().PadLeft(3,'0');
                    pkFailReturn.PackHead = new PackHead(pkHead.PayWay, pkHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                    CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
                    LogHelper.WriteInfoLog("GenericPayment方法返回020响应码");
                    return;
                }
                PackRequestPayment payment = (PackRequestPayment)obj;
                payment.PackHead=pkHead;
                Payment.ProcessPayment(payment, client);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("GenericPayment方法-生成缴费信息实体对象异常：" + ex.Message, ex);
                pkFailReturn.ResponseCode = ((int)ResponseCode.其他错误).ToString().PadLeft(3,'0');
                pkFailReturn.PackHead = new PackHead(pkHead.PayWay, pkHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
            }
          
        }
    }
}
